package badugi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import org.omg.CORBA.Request;

import client.Client;
import client.ClientThread;

/**
 * Created by grzegorz on 11/23/14.
 * A bot able to play the game.
 */
public class Bot {
	private ClientThread clientThread;
	private int balance;
	private int[] otherBalances;
	private int bid;
	private int[] otherBids;
	private Card[] cards = new Card[ 4 ];
	private PrintWriter out;
	private BufferedReader in;

	public Bot( String address, String port ) {
		try {
			clientThread = new ClientThread( address, port );
			out = clientThread.getOutput();
			in = clientThread.getInput();
			Thread clientTask = new Thread( clientThread );
			int slot = Integer.parseInt( in.readLine() );
			int sb = Integer.parseInt( in.readLine() );
			int bb = Integer.parseInt( in.readLine() );
			int balance = Integer.parseInt( in.readLine() );
			out.println( "S" );
			while( true ) {
				String header = in.readLine();
				if (header.equals("YourTurn")){
					if (header.equals("You bid.")){
						out.print("R"+balance);
					}
					else{
						changeCards();
					}
				}else{
					switch (header) {
					case "Winners":
						String[] news = new String[3];
						try{
							for (int i = 0; i < 3; i++){
								news[i] = Client.clientThread.getInput().readLine();
							}
						}catch(IOException e){
							System.out.println("IOE winners");
						};
						try{
							int id = Integer.parseInt(news[0]);
							int win = Integer.parseInt(news[1]);
							int nbalance = Integer.parseInt(news[2]);
							if (id == slot){
								balance = nbalance;
							}
						}catch(NumberFormatException e){
							System.out.println("NFE winners");
						}
						break;
					case "Game":
						String[] news2 = new String[6];
						try{
							for (int i = 0; i < 6; i++){
								news2[i] = Client.clientThread.getInput().readLine();
							}
						}catch(IOException e){
							System.out.println("IOE game");
						};
						try{
							int id = Integer.parseInt(news2[0]);
							boolean changingCards = Boolean.parseBoolean(news2[1]);
							int cards = Integer.parseInt(news2[2]);
							char action = news2[3].charAt(0);
							int bid = Integer.parseInt(news2[4]);
							int nbalance = Integer.parseInt(news2[5]);
							if (slot == id){
								balance = nbalance;
							}
						}catch(NumberFormatException e){
							System.out.println("NumberFormatException game");
						}
						break;
					case "SetUp":
						String[] news1 = new String[4];
						try{
							for (int i = 0; i < 4; i++){
								news1[i] = Client.clientThread.getInput().readLine();
							}
						}catch(IOException e){
							System.out.println("IOE setup");
						};
						try{
							int id = Integer.parseInt(news1[0]);
							String status= news1[1];
							int bid = Integer.parseInt(news1[2]);
							int nbalance = Integer.parseInt(news1[3]);
							if (id == slot){
								balance = nbalance;
							}
						}catch(NumberFormatException e){
							System.out.println("NFE setup");
						}
						break;
					default:
						break;
				}
				}
			}
		} catch( Exception e ) {
			System.out.println( "ClientThread threw an exception." );
		}
	}
	private void changeCards(){
		ArrayList<Card[]>handPS = HandEvaluation.powerSet(this.cards);
		HandEvaluation.sortArray(handPS);
		Card[] bestHand = handPS.get(0);
		boolean isIn = false;
		String request = "";
		for (int i = 0; i < 4; i++){
			for (int j = 0; j < 4; j++){
				if (cards[i].getSuit() == bestHand[j].getSuit() && cards[i].getRank() == bestHand[j].getRank()){
					isIn = true;
					break;
				}
			}
			if (!isIn){
				request += i;
				isIn = false;
			}
		}
		out.println(request);
	}

	public static void main( String[] args ) {
		new Bot( "localhost", "4444" );
	}
}